using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Dto;
using Yitter.IdGenerator;

namespace Medical.WriteApi.Appcontext.CommandHandler
{
	public class CreateRoleCommandHandler : IRequestHandler<CreateRoleCommand, RoleDto>
	{
		private readonly IBaseRepository<Role> _repository;

		public CreateRoleCommandHandler(IBaseRepository<Role> repository)
		{
			_repository = repository;
		}

		public async Task<RoleDto> Handle(CreateRoleCommand request, CancellationToken cancellationToken)
		{
			var role = new Role { RoleId = YitIdHelper.NextId(), Name = request.Name, Description = request.Description };
			await _repository.InsertAsync(role);
			return new RoleDto { RoleId = role.RoleId, Name = role.Name, Description = role.Description, IsActive = role.IsActive, CreatedAt = role.CreatedAt, UpdatedAt = role.UpdatedAt };
		}
	}
}
